<template>
  <div>
    <layout-box one="通知发布" two="发布通知">
      <el-button type="primary" @click="rightsDialogVisible = true"
        >发布通知</el-button
      >
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="标题" prop="messageTitle"></el-table-column>
        <el-table-column label="内容" prop="messageContext"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteInform(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </layout-box>

    <el-dialog title="发布通知" :visible.sync="rightsDialogVisible">
      <el-form :model="addressForm" ref="addressFormRef" label-width="100px">
        <el-form-item label="标题" prop="messageTitle">
          <el-input placeholder="请输入标题" v-model="addressForm.messageTitle">
          </el-input>
        </el-form-item>
        <el-form-item label="发布内容" prop="messageContext">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="addressForm.messageContext"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="settingRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "notice",
  data() {
    return {
      rightsDialogVisible: false,
      addressForm: {},
      orderList: [],
    };
  },
  created() {
    this.getinform();
  },
  methods: {
    // 查询发布的通知
    async getinform() {
      const { data: res } = await this.$http.get("/message/getByUser");
      if (res.code != 200) {
        return this.$message.error("查询消息失败");
      } else {
        this.orderList = res.data;
      }
      console.log(res);
    },
    // 发布通知
    async settingRights() {
      console.log(this.express);
      const { data: res } = await this.$http.post("/message/add", {
        messageTitle: this.addressForm.messageTitle,
        messageContext: this.addressForm.messageContext,
      });
      if (res.code != 200) {
        return this.$message.error("发布通知失败");
      }
      this.$message.success("发布通知成功");
      this.rightsDialogVisible = false;
      this.getinform();
      console.log(res);
    },
    // 删除消息
    async deleteInform(id) {
      const { data: res } = await this.$http.delete("/message/delete/" + id);
      if (res.code != 200) {
        return this.$message.error("删除消息失败");
      }
      this.$message.success("删除消息成功");
      this.getinform();
    },
  },
};
</script>
<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>